function updateStatus(username) {
    fetch(`/admin/thread_status?username=${username}`)
    .then(response => response.json())
    .then(data => {
        document.getElementById(`status-${username}`).innerText = data.status;
    });
}

function stopThread(username) {
    fetch('/admin/stop_thread', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ username: username })
    })
    .then(response => response.json())
    .then(data => {
        alert(data.message);
        updateStatus(username);
    });
}

// Update status on page load for all threads
document.querySelectorAll("[id^='status-']").forEach(elem => {
    let username = elem.id.split('-')[1];
    updateStatus(username);
});
